
cc.Class({
    extends: cc.Component,

    properties: {
       gates:[cc.Node],
       selected:cc.Node,
       level:[cc.Node],
       tip:cc.Label,
       mask:cc.Node,
       coin:cc.Label
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        SoundMgr.playMusic('./Audio/bgm_gates');
        this.arr=[[cc.v2(0,0),1],[cc.v2(-250,-150),0.8],[cc.v2(0,-250),0.9],[cc.v2(250,-150),0.8],[cc.v2(250,150),0.7],[cc.v2(0,250),0.5]];
        this.pass=[];
        this.gateUnlock=global.Data.plane.gate
        this.mask.active=false;
    },

    start () {
        global.gates=1;
        for(let i=0;i<6;i++){
            this.gates[i].children[0].active=this.gateUnlock[i]=='1'?false:true;
        }
        this.unlock=null;
        for(let i=0;i<this.gateUnlock.length;i++){
            if(this.gateUnlock[i]==1){
                this.unlock=i
            }
        }
        global.gates=this.unlock+1;
        global.unlock=this.unlock+1;
        for(let i=0;i<6;i++){
            this.level[i].active=false;
        }
        this.level[global.gates-1].active=true;
        this.newArr0=[];
        for(let i=this.unlock;i<this.arr.length;i++){
            this.newArr0.push(this.gates[i]);
        }
        for(let i=0;i<this.unlock;i++){
            this.newArr0.push(this.gates[i]);
        }
        this.gates=this.newArr0;
        for(let i=0;i<6;i++){
            this.gates[i].setPosition(this.arr[i][0]);
            this.gates[i].setScale(this.arr[i][1]);
        }
        this.coinNumber();
    },

    coinNumber(){
        if(global.gates>=global.unlock){
            this.coin.string='过关获得'+(1000+(global.gates-1)*500)+'金币，首次过关奖励翻倍'
        }else{
            this.coin.string='过关获得'+(1000+(global.gates-1)*500)+'金币'
        }
    },

    moveGates(){
        global.gates =parseInt(this.gates[0].name.split('_')[1]);
        this.selected.active=false;
        this.mask.active=true;
        this.coinNumber();
        for(let i=0;i<6;i++){
            this.level[i].active=false;
            this.gates[i].runAction(cc.sequence(cc.spawn(cc.moveTo(0.2,this.arr[i][0]),cc.scaleTo(0.2,this.arr[i][1])),cc.callFunc(()=>{
                this.mask.active=false;
                this.selected.active=true
            })));
        }
        this.level[global.gates-1].active=true;
        
    },
    onClick(event,data){
        if(data == 'back'){
            this._back();
        }else if(data == 'start'){
            this._startGame();
        }
    },
    _back(){
        SoundMgr.playSound('Audio/button');
        SoundMgr.playMusic('./Audio/bgm_main');
        global.UIMgr.popUI();
    },
    _startGame(){
        if(global.Data.isFirst==undefined || global.Data.isFirst==true){
            global.gates=-1
        }
        SoundMgr.playSound('Audio/button')
        if(this.gates[0].children[0].active===false){
            this.tip.string=''
            SoundMgr.playSound('Audio/DoorClose')
            var door=cc.find('Canvas/door');
            door.active = true;
            door.js.moveIn(()=>{
                global.Loader.loadResources('GameLoop',function(err,res){
                    global.UIMgr.clearNode();
                    global.GameLoop.enterGameScene();
                    let number=global.gates!=-1?global.gates:1
                    SoundMgr.playMusic('./Audio/bgm_'+number)
                })
            });
        }else{
            // cc.log('通过上一关才能解锁')
            global.EJMgr.showToast('通过上一关才能解锁')
        }
        
    },
    onEventChange(event,data){
       if(data == 'left'){
           this._left();
       }else if(data == 'right'){
           this._right();
       }
    },

    _left(){
        SoundMgr.playButtonEffect();
        this.newArr=[];
        for(let i=0;i<this.arr.length;i++){
            if(this.arr[i+1]){
                this.newArr.push(this.gates[i+1]);
            }else{
                this.newArr.push(this.gates[0]);
            }
        }
        SoundMgr.playSound('Audio/Sgates')
        this.gates=this.newArr;
        this.moveGates()
        if(this.gates[0].children[0].active===true){
            this.tip.string='第'+(parseInt(this.gates[0].name.split('_')[1])-1)+'关未通过'
        }else{
            this.tip.string=''
        }
    },
    _right(){
        SoundMgr.playButtonEffect();
        this.newArr=[];
        for(let i=0;i<this.arr.length;i++){
            if(i<5){
                this.newArr.push(this.gates[i]);
            }else{
                this.newArr.unshift(this.gates[5]);
            }
        }
        SoundMgr.playSound('Audio/Sgates')
        this.gates=this.newArr;
        this.moveGates()
        if(this.gates[0].children[0].active===true){
            this.tip.string='第'+(parseInt(this.gates[0].name.split('_')[1])-1)+'关未通过'
        }else{
            this.tip.string=''
        }
    }
    // update (dt) {},
});
